local Musha_Skills =
	Class(
	function(self, inst)
		inst.entity:AddLight()

		self.inst = inst
		self.datatransporter = net_string(inst.GUID, "musha_skills.datatransporter", "musha_skills_ondata")
		self.musha_shadowhide = net_bool(inst.GUID, "musha_skills.musha_shadowhide", "musha_shadowhidedirty")
		self.skill_point = {
			Sleep = net_tinybyte(inst.GUID, "musha_skills.Sleep"),
			Sniff = net_tinybyte(inst.GUID, "musha_skills.Sniff"),
			Music = net_tinybyte(inst.GUID, "musha_skills.Music"),
			ShadowHide = net_tinybyte(inst.GUID, "musha_skills.ShadowHide"),
			ElectricAttack = net_tinybyte(inst.GUID, "musha_skills.ElectricAttack"),
			ElectricShield = net_tinybyte(inst.GUID, "musha_skills.ElectricShield"),
			PowerAttack = net_tinybyte(inst.GUID, "musha_skills.PowerAttack"),
			Valkyr = net_tinybyte(inst.GUID, "musha_skills.Valkyr"),
			Berserk = net_tinybyte(inst.GUID, "musha_skills.Berserk"),
			Unassigned = net_smallbyte(inst.GUID, "musha_skills.Unassigned")
		}
		for k, v in pairs(self.skill_point) do
			v:set_local(0)
		end
		--ThePlayer.components.musha_skills.skill_point.Unassigned:set(30)
		self.skill_point.Unassigned:set_local(1)
		setmetatable(
			self.skill_point,
			{
				__index = {
					PoisonAttack = self.skill_point.ElectricAttack,
					FireShield = self.skill_point.ElectricShield
				}
			}
		)

		if not inst.components.musha_keyhandler then
			inst:AddComponent("musha_keyhandler")
		end
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.LEVEL_INFO, "Level_Info")
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.SKILL_UI, "Skill_UI")
		inst.components.musha_keyhandler:AddActionListener(
			"musha",
			inst.musha_settings.Keys.SPECIALATTACKSWITCH,
			"SpecialAttackSwitch"
		)
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.ELECTRICSHIELD, "ElectricShield")
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.SING, "Sing")
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.SNIFF, "Sniff")
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.SHADOWHIDE, "ShadowHide")
		inst.components.musha_keyhandler:AddActionListener("musha", inst.musha_settings.Keys.SLEEP_SWITCH, "Sleep_Switch")

		if TheWorld.ismastersim and TheNet:GetServerIsDedicated() then
			self.classified = inst.player_classified
		else
			if TheWorld.ismastersim then
				self.classified = inst.player_classified
			elseif self.classified == nil and inst.player_classified ~= nil then
				self:AttachClassified(inst.player_classified)
			end
			inst:ListenForEvent(
				"musha_skills_ondata",
				function(inst)
					local musha_skill_menu = inst.HUD.controls.musha_skill_menu
					if musha_skill_menu.shown then
						musha_skill_menu.skill_point["Unassigned"] =
							musha_skill_menu.skill_point["Unassigned"] + self.skill_point["Unassigned"]:value() - self.musha_unassigned
						musha_skill_menu.skill_point_new["Unassigned"] =
							musha_skill_menu.skill_point_new["Unassigned"] + self.skill_point["Unassigned"]:value() - self.musha_unassigned
						musha_skill_menu.skill_point_unassigned:SetString(
							STRINGS.MUSHA_SKILLS.UNASSIGNED .. "  " .. musha_skill_menu.skill_point["Unassigned"]
						)
					end
					self.musha_unassigned = self.skill_point["Unassigned"]:value()
				end
			)
			inst:ListenForEvent(
				"musha_shadowhidedirty",
				function(inst)
					musha_print("musha_shadowhidedirty")
					inst:Show()
					inst.DynamicShadow:Enable(true)
				end
			)
		end
	end,
	nil,
	nil
)

function Musha_Skills:OnRemoveFromEntity()
	if self.classified ~= nil then
		if TheWorld.ismastersim then
			self.classified = nil
		else
			self.inst:RemoveEventCallback("onremove", self.ondetachclassified, self.classified)
			self:DetachClassified()
		end
	end
end

Musha_Skills.OnRemoveEntity = Musha_Skills.OnRemoveFromEntity

function Musha_Skills:AttachClassified(classified)
	self.classified = classified
	self.ondetachclassified = function()
		self:DetachClassified()
	end
	self.inst:ListenForEvent("onremove", self.ondetachclassified, classified)
end

function Musha_Skills:DetachClassified()
	self.classified = nil
	self.ondetachclassified = nil
end

function Musha_Skills:ShadowHide(data)
	musha_print("send")
	self.musha_shadowhide:set(not self.musha_shadowhide:value())
end

function Musha_Skills:SetData(name, data)
	if TheNet:GetIsServer() then
		if self.inst.components.musha_skills then
			self.inst.components.musha_skills:Updata(name, data)
		end
	else
		self.skill_point[name]:set_local(data)
		SendModRPCToServer(MOD_RPC["musha"]["Upload_SkillData"], name, data)
	end
end

function Musha_Skills:GetData(name)
	return self.skill_point[name]:value()
end

return Musha_Skills
